const fs = require('node:fs/promises');

(async ()=>{
    const fileHandler = await fs.open('./src.txt', 'r');
    const targetFileHandler = await fs.open('./dest.txt', 'w')
    const readStream = fileHandler.createReadStream({
        // highWaterMark: 64 * 1024,
    });
    const writeStream = targetFileHandler.createWriteStream();
    let split = '';
    readStream.on('data', (chunks)=>{
        // 将超大文件内容划分成偶数内容和基数内容
        const numbers = chunks.toString('utf-8').split('  ');
        // 判断每个数据块最后一个字符串是否被分割，如果分割则要重新合并
        if ( Number(numbers[0]) + 1 !== Number(numbers[1])){
            if ( split ) {
                numbers[0] = split.trim() + numbers[0].trim();
            }
        }
        if ( Number(numbers[numbers.length-2]) + 1 !== Number(numbers[numbers.length-1])) {
            split = numbers.pop();
        }
        console.log(numbers);
        numbers.forEach(number=>{
            let num = Number(number);
            if ( num % 2 === 0 ) {
                if ( !writeStream.write(` ${num} `)) {
                    readStream.pause();
                }
            }
        })
        // if ( !writeStream.write(chunks)) {
        //     readStream.pause();
        // }
    })
    writeStream.on('drain', ()=>{
        readStream.resume();
    })
})()